import wx
from wx.lib.filebrowsebutton import FileBrowseButton

##  Contains the preferences window, in which the location of the files can be specified.
class PreferencesDialog(wx.Dialog):

    ##  The contructor of PreferencesDialog. GUI elements of the window are setup here.
    #   @param[in]  gui     Gui instance.
    #   @param[in]  **kwds  Automatically passed parameter.
    def __init__(self, gui, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.THICK_FRAME
        wx.Dialog.__init__(self, gui, **kwds)

        wildcardDat = "DAT file (*.dat)|*.dat|" \
                      "All files (*.*)|*.*"

        wildcardIni = "INI file (*.ini)|*.ini|" \
                      "All files (*.*)|*.*"

        # Initialize components.
        stylusLabel = wx.StaticText(self, -1, "Stylus file:")
        ##  Location of the stylusFile, contains data for the pointer sensor.
        #   Default location: Data\Stylus.dat.
        self.stylusFile = FileBrowseButton(self, -1,
                                           labelText = "", fileMask = wildcardDat,
                                           size = (450,-1))
        calibrationLabel = wx.StaticText(self, -1, "Calibration file:")
        ##  Location of the calibration file, containing data concerning calibration data for the elektromagnetic field of the Flock of Birds system.
        #   Default location: Data\Calibration.dat.
        self.calibrationFile = FileBrowseButton(self, -1,
                                                labelText = "", fileMask = wildcardDat,
                                                size = (450,-1))
        blPositionsLabel = wx.StaticText(self, -1, "3D Bony landmark positions file:")
        ##  Location of the bony landmark position file, containing coordinates for the bony landmarks in the bone models.
        #  Default location:  \BLPositions.ini.
        self.blPositionsFile = FileBrowseButton(self, -1,
                                                labelText = "", fileMask = wildcardIni,
                                                size = (450,-1))

        configLabel = wx.StaticText(self, -1, "Configuration file:")
        ##  Location of the confid file, containing coordinates for the bony landmarks in the bone models.
        #  Default location:  \Config.ini.
        self.configFile = FileBrowseButton(self, -1,
                                                labelText = "", fileMask = wildcardIni,
                                                size = (450,-1))
        static_line_1 = wx.StaticLine(self, -1)
        saveButton = wx.Button(self, wx.ID_OK, "Save")
        cancelButton = wx.Button(self, wx.ID_CANCEL)            

        # Set Properties.
        self.SetTitle("Preferences")
        self.SetIcon(wx.Icon(gui.main.config.root+"\\Images\\icon.ico", wx.BITMAP_TYPE_ICO, 32, 32))
        self.stylusFile.SetValue(gui.main.prefReader.stylusFile)
        self.calibrationFile.SetValue(gui.main.prefReader.calibrationFile)
        self.blPositionsFile.SetValue(gui.main.prefReader.blPositionsFile)
        self.configFile.SetValue(gui.main.prefReader.configFile)

        # Do Layout.
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1 = wx.FlexGridSizer(4, 1, 0, 0)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1_copy = wx.FlexGridSizer(2, 2, 10, 10)
        grid_sizer_1_copy.Add(stylusLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 0)
        grid_sizer_1_copy.Add(self.stylusFile, 0, wx.EXPAND, 0)
        grid_sizer_1_copy.Add(calibrationLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 0)
        grid_sizer_1_copy.Add(self.calibrationFile, 0, wx.EXPAND, 0)
        grid_sizer_1_copy.Add(blPositionsLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 0)
        grid_sizer_1_copy.Add(self.blPositionsFile, 0, wx.EXPAND, 0)
        # Watch out here.
        grid_sizer_1_copy.Add(configLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 0)
        grid_sizer_1_copy.Add(self.configFile, 0, wx.EXPAND, 0)
        grid_sizer_1_copy.AddGrowableCol(1)
        grid_sizer_1.Add(grid_sizer_1_copy, 1, wx.ALL|wx.EXPAND, 10)
        grid_sizer_1.Add(static_line_1, 0, wx.EXPAND, 0)
        sizer_2.Add(saveButton, 0, wx.RIGHT|wx.ALIGN_BOTTOM, 5)
        sizer_2.Add(cancelButton, 0, wx.ALIGN_BOTTOM, 0)
        grid_sizer_1.Add(sizer_2, 1, wx.ALL|wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM, 5)
        grid_sizer_1.AddGrowableRow(0)
        grid_sizer_1.AddGrowableCol(0)
        sizer_1.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        
        self.Bind(wx.EVT_BUTTON, self.OnSave, saveButton)

    ##  Event handler. Saves the preference file locations.
    def OnSave(self, event):
        if self.stylusFile.GetValue() == '' or self.calibrationFile.GetValue() == '' or self.blPositionsFile.GetValue() == '':
            msg = "Not all fields are filled in."
            dlg = wx.MessageDialog(self, msg, 'Error', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
        else:
            event.Skip()